local jLootz = LibStub("AceAddon-3.0"):NewAddon("jLootz", "AceHook-3.0")

jLootz_DONT_DISABLE_BASE_UI = true

local defaults = {
	profile = {
		EnabledModules = {
			Frame = true,
			Group = true,
			History = false,
			Master = false,
			Monitor = false,
			Confirm = true,
		},
	},
}


local function Initialize_Frame(self)
	local f = self:GetModule("Frame", true)
	if not f and IsAddOnLoadOnDemand("jLootz_Frame") then
		self:RegisterEventNotification("LOOT_OPENED", true)
		if not jLootz_DONT_DISABLE_BASE_UI then
			LootFrame:UnregisterEvent("LOOT_OPENED")
		end
		self.RegisterCallback("jLootz:Frame", "LOOT_OPENED", function (event, ...)
			LoadAddOn("jLootz_Frame")
			local f = self:GetModule("Frame")
			if f then
				f[event](event, ...)
			else
				LootFrame:RegisterEvent("LOOT_OPENED")
				LootFrame_OnEvent("LOOT_OPENED") -- Is the taint problematic ?
			end
			self.UnregisterCallback("jLootz:Frame", "LOOT_OPENED")
			self:RegisterEventNotification("LOOT_OPENED", false)
		end)
	end
end

local function Initialize_Group(self)
	local f = self:GetModule("Group", true)
	if not f and IsAddOnLoadOnDemand("jLootz_Group") then
		self:RegisterEventNotification("START_LOOT_ROLL", true)
		if not jLootz_DONT_DISABLE_BASE_UI then
			UIParent:UnregisterEvent("START_LOOT_ROLL")
			UIParent:UnregisterEvent("CONFIRM_LOOT_ROLL")
		end
		self.RegisterCallback("jLootz:Group", "START_LOOT_ROLL", function (event, ...)
			self:RegisterEventNotification("START_LOOT_ROLL", false)
			LoadAddOn("jLootz_Group")
			local f = self:GetModule("Group")
			if f then
				self:RegisterLootRoolEvents()
				f[event](event, ...)
			else
				UIParent:RegisterEvent("START_LOOT_ROLL")
				UIParent:RegisterEvent("CONFIRM_LOOT_ROLL")
				GroupLootFrame_OpenNewFrame(...) -- Is the taint problematic ?
			end
			self.UnregisterCallback("jLootz:Group", "START_LOOT_ROLL")
			self.RegisterLootRoolEvents = nil
		end)
	end
end

local function Initialize_History(self)
	local f = self:GetModule("History", true)
	if not f and IsAddOnLoadOnDemand("jLootz_History") then
		local handler = function (event, ...)
			LoadAddOn("jLootz_History")
			local f = self:GetModule("History")
			if f then
				f[event](event, ...)
			end
			self.UnregisterCallback("jLootz:History", "LOOT_ITEM")
			self.UnregisterCallback("jLootz:History", "LOOT_MONEY")
		end
		self.RegisterCallback("jLootz:History", "LOOT_ITEM", handler)
		self.RegisterCallback("jLootz:History", "LOOT_MONEY", handler)
	end
end

local function Initialize_Master(self)
	local f = self:GetModule("Master", true)
	if not f and IsAddOnLoadOnDemand("jLootz_Master") then
		self:RegisterEventNotification("OPEN_MASTER_LOOT_LIST", true)
		if not jLootz_DONT_DISABLE_BASE_UI then
			LootFrame:UnregisterEvent("OPEN_MASTER_LOOT_LIST")
		end
		self.RegisterCallback("jLootz:Master", "OPEN_MASTER_LOOT_LIST", function (event, ...)
			LoadAddOn("jLootz_Master")
			local f = self:GetModule("Master")
			if f then
				f[event](event, ...)
			else
				LootFrame:RegisterEvent("OPEN_MASTER_LOOT_LIST")
				LootFrame_OnEvent("OPEN_MASTER_LOOT_LIST") -- Is the taint problematic ?
			end
			self.UnregisterCallback("jLootz:Master", "OPEN_MASTER_LOOT_LIST")
			self:RegisterEventNotification("OPEN_MASTER_LOOT_LIST", false)
		end)
	end
end

local function Initialize_Monitor(self)
	local f = self:GetModule("Monitor", true)
	if not f and IsAddOnLoadOnDemand("jLootz_Monitor") then
		self.RegisterCallback("jLootz:Monitor", "LOOT_ITEM", function (event, ...)
			LoadAddOn("jLootz_Monitor")
			local f = self:GetModule("Monitor")
			if f then
				f[event](event, ...)
			end
			self.UnregisterCallback("jLootz:Monitor", "LOOT_ITEM")
		end)
	end
end

function jLootz:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("jLootzDB", defaults)
	self.db.RegisterCallback(self, "OnProfileChanged", "UpdateModuleConfigs")
	self.db.RegisterCallback(self, "OnProfileCopied", "UpdateModuleConfigs")
	self.db.RegisterCallback(self, "OnProfileReset", "UpdateModuleConfigs")

	self:UpdateModuleConfigs()
	self.OnInitialize = nil
end

local confirm
function jLootz:UpdateModuleConfigs()
	local enabled = self.db.profile.EnabledModules
	for name, module in self:IterateModules() do
		if enabled[name] then
			module:Enable()
		else
			module:Disable()
		end
	end
	if enabled.Frame then
		Initialize_Frame(self)
	end
	if enabled.Group then
		Initialize_Group(self)
	end
	if enabled.History then
		Initialize_History(self)
	end
	if enabled.Master then
		Initialize_Master(self)
	end
	if enabled.Monitor then
		Initialize_Monitor(self)
	end
	if not enabled.Confirm then
		confirm = nil
	end
end

function jLootz:GetConfirmModule()
	if not confirm and self.db.profile.EnabledModules.Confirm then
		LoadAddOn("jLootz_Confirm")
		confirm = self:GetModule("Confirm", true)
	end
	return confirm
end

_G.jLootz = jLootz
